package org.itboys.seller;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.itboys.baseobject.entity.DataDictionary;
import org.itboys.baseobject.service.DataDictionaryService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.shop.dto.TouristEnum;
import org.itboys.shop.dto.TouristType;
import org.itboys.shop.entity.TouristSpot;
import org.itboys.shop.service.TouristService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 商家
 * @author weisky
 *
 */
@RestController
@RequestMapping("/tourist")
public class TouristController extends BaseController{

    @Autowired
    private TouristService touristService;
    @Autowired
    private DataDictionaryService dataDictionaryService;

    /**
     * 跳到商家管理页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/toListVM")
    public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("/seller/tourist-list");
    }

    @RequestMapping("/list")
    public void list(@Param("type")Integer type, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
        String name = (String) param.get("name");
        if(StringUtils.isNotBlank(name)){//根据商家名称  模糊查询
            param.put("name", AceUtils.getPattern(name));
        }
        Page<TouristSpot> page = touristService.sellerListByPage(param, request);
        for (TouristSpot seller : page.getData()) {
            DataDictionary dictionary = dataDictionaryService.getById(seller.getTouristType());
            if(dictionary != null) {
                seller.setTypeName(dictionary.getText());
            }
        }
        JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
    }

    /**
     * 返回所有的列表
     * @param response
     * @param request
     */
    @RequestMapping("/allList")
    public void allList(HttpServletResponse response,
                        HttpServletRequest request) {
        final Map<String, Object> sqlMap = QueryParamUtils.builderQueryMap(request);
        List<TouristSpot> list = touristService.list(sqlMap);
        AjaxUtils.renderJson(response, list);
    }

    /**
     * 获取所有分类类型
     * @return
     */
    @RequestMapping("/findAllType")
    public void findAllProvince(HttpServletRequest request , HttpServletResponse response){
        List<TouristType> typeList = Lists.newArrayList();
        for (TouristEnum en : TouristEnum.values()) {
            typeList.add(new TouristType(en.getType(), en.getName()));
        }
        AjaxUtils.renderJson(response, typeList);
    }

    /**
     * 新增或者修改
     * @param seller
     * @param logos:商家logo
     * @param mainImgs：主图
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(TouristSpot seller,
                     @RequestParam(required=false) MultipartFile logos,
                     @RequestParam(required=false) MultipartFile mainImgs,
                     HttpServletRequest request, HttpServletResponse response){
        boolean result = touristService.insert(seller, logos, mainImgs);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 修改商家的状态
     * @param sellerId
     * @param state
     * @param response
     */
    @RequestMapping("/updateState")
    public void updateState(Long sellerId, int state, HttpServletResponse response){
        boolean result = touristService.updateState(sellerId, state);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 获取一条数据
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/getById")
    public void getById(Long id, HttpServletRequest request, HttpServletResponse response){
        AjaxUtils.renderJson(response, touristService.getById(id));
    }

    /**
     * 删除一条数据
     * @param id
     * @param request
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(Long id, HttpServletRequest request, HttpServletResponse response){
        boolean result = touristService.dele(id);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }


}
